package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.ByCity;
import com.baseweb.webadmin.core.model.pojo.ByOpenCity;
import com.baseweb.webadmin.core.model.pojo.ByProvince;
import com.baseweb.webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by dsy62 on 2017-08-15.
 */
@Controller
@RequestMapping(value = "/appApiCity")
public class CityAppController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 获取开通省。城市
     */
    @ResponseBody
    @RequestMapping(value = "/findProvinceOpen", method = RequestMethod.POST)
    public Map<String, Object> findProvinceOpen(@RequestParam("where") String where){
        List<ByProvince> byProvinceList = new ArrayList<ByProvince>();
        List<ByOpenCity> byOpenCityList = userService.findAllProvinceCityListByWhere(where);
        String province = "";
        ByProvince provinces = new ByProvince();
        int i = 0;
        List<ByCity> cityList = new ArrayList<ByCity>();
        for(ByOpenCity byOpenCity : byOpenCityList){
            ByCity byCity = new ByCity();
            byCity.setName(byOpenCity.getCity());

            if(province.equals(byOpenCity.getProvince())){
                cityList.add(byCity);
                provinces.setByCityList(cityList);
            }else {
                if(!"".equals(province) || i == 1){
                    byProvinceList.add(provinces);
                }
                provinces = new ByProvince();
                cityList = new ArrayList<ByCity>();
                cityList.add(byCity);

                provinces.setName(byOpenCity.getProvince());
                provinces.setByCityList(cityList);
            }

            if(i == (byOpenCityList.size()-1)){
                byProvinceList.add(provinces);
            }
            province = byOpenCity.getProvince();
            i++;
        }

        return success(byProvinceList,"查询成功");
    }

    /**
     * 根据市查询区
     */
    @ResponseBody
    @RequestMapping(value = "/findAreaByCity", method = RequestMethod.POST)
    public Map<String, Object> findAreaByCity(@RequestParam("city") String city){
        List<ByOpenCity> byOpenCityList = userService.findAreaByCity(city);
        return success(byOpenCityList,"查询成功");
    }
}
